package bean;

public class ListNode {
    public int val;
    public ListNode next;

    public ListNode(int val) {
        this.val = val;
    }

    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }

    public static ListNode createListNode(int[] arr){
        ListNode head = new ListNode(arr[0]);

        ListNode last = head;
        for (int i = 1; i < arr.length; i++){
            last.next = new ListNode(arr[i]);
            last = last.next;
        }

        return head;
    }

    public String toString(){
        StringBuffer sb = new StringBuffer();
        sb.append(val + "  ");
        ListNode nextNode = next;
        while (nextNode != null){
            sb.append(nextNode.val + "  ");
            nextNode = nextNode.next;
        }

        return sb.toString();
    }

    public static ListNode createListNodes(int arr[]){
        ListNode head = new ListNode(arr[0]);

        ListNode lastNode = head;
        for (int i = 1; i < arr.length; i++){
            lastNode.next = new ListNode(arr[i]);
            lastNode = lastNode.next;
        }

        return head;
    }

    public static void printNodeList(ListNode node){
        while (node != null){
            System.out.print(node.val + "  ");
            node = node.next;
        }
    }
}
